<template>
  <div class="user-profile-popup" v-if="visible" @click.self="close">
    <div class="popup-content">
      <div class="popup-header">
        <h3>{{ userProfile.username }} 的个人资料</h3>
        <el-button link @click="close" icon="Close">关闭</el-button>
      </div>
      
      <div class="profile-info">
        <div class="avatar-section">
          <el-avatar :size="80" :src="userProfile.avatar || '/default-avatar.png'" />
          <h4>{{ userProfile.firstname }} {{ userProfile.lastname }}</h4>
        </div>
        
        <div class="info-section">
          <div class="info-item">
            <label>用户名:</label>
            <span>{{ userProfile.username }}</span>
          </div>
          <div class="info-item">
            <label>邮箱:</label>
            <span>{{ userProfile.email }}</span>
          </div>
          <div class="info-item">
            <label>学号:</label>
            <span>{{ userProfile.studentnumber || '未设置' }}</span>
          </div>
          <div class="info-item">
            <label>电话:</label>
            <span>{{ userProfile.phonenumber || '未设置' }}</span>
          </div>
          <div class="info-item">
            <label>个人简介:</label>
            <span>{{ userProfile.aboutme || '这个人很懒，什么都没写~' }}</span>
          </div>
        </div>
      </div>
      
      <div class="popup-actions">
        <el-button 
          v-if="isCurrentUser" 
          type="primary" 
          @click="editProfile"
        >
          编辑个人资料
        </el-button>
        <el-button 
          v-else 
          type="primary" 
          @click="sendMessage"
        >
          发送私信
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

export default {
  name: 'UserProfilePopup',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    userProfile: {
      type: Object,
      default: () => ({})
    }
  },
  emits: ['close'],
  setup(props, { emit }) {
    const router = useRouter()
    const authStore = useAuthStore()
    
    const isCurrentUser = computed(() => {
      return authStore.user?.username === props.userProfile.username
    })
    
    const close = () => {
      emit('close')
    }
    
    const editProfile = () => {
      close()
      router.push('/profile')
    }
    
    const sendMessage = () => {
      close()
      router.push({
        path: '/private-message',
        query: { to: props.userProfile.username }
      })
    }
    
    return {
      isCurrentUser,
      close,
      editProfile,
      sendMessage
    }
  }
}
</script>

<style scoped>
.user-profile-popup {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 2000;
}

.popup-content {
  background-color: white;
  border-radius: 12px;
  padding: 24px;
  max-width: 500px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.popup-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #eee;
}

.popup-header h3 {
  margin: 0;
  color: #333;
}

.profile-info {
  margin-bottom: 20px;
}

.avatar-section {
  text-align: center;
  margin-bottom: 20px;
}

.avatar-section h4 {
  margin: 10px 0 0 0;
  color: #333;
}

.info-section {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.info-item {
  display: flex;
  align-items: flex-start;
}

.info-item label {
  font-weight: bold;
  color: #666;
  min-width: 80px;
  margin-right: 10px;
}

.info-item span {
  color: #333;
  flex: 1;
  word-break: break-word;
}

.popup-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  padding-top: 15px;
  border-top: 1px solid #eee;
}
</style> 